package com.ruyuan.dfs.model.common;

import com.google.protobuf.InvalidProtocolBufferException;
import com.ruyuan.dfs.common.PacketType;
import com.ruyuan.dfs.server.NettyPacket;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.SocketChannel;

import java.io.File;
import java.util.Set;

/**
 * @description:
 * @Author gckj
 * @Email:
 * @Date: 2024/8/2 14:42
 * @Version 1.0
 */
public class FileChannelHandler extends AbstractChannelHandler{

    private FileReceiveHandler fileReceiveHandler;

    public FileChannelHandler(){
        fileReceiveHandler = new FileReceiveHandler();
    }

    @Override
    protected boolean handlePackage(ChannelHandlerContext ctx, NettyPacket request) throws Exception {
        PacketType packetType = PacketType.getEnum(request.getPacketType());
        RequestWrapper requestWrapper = new RequestWrapper(ctx, request);
        switch (packetType) {
            case TRANSFER_FILE:
                handleFileTransferRequest(requestWrapper);
                break;
            case GET_FILE:
                handleGetFileRequest(requestWrapper);
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    protected Set<Integer> interestPackageTypes() {
        return null;
    }

    private void handleGetFileRequest(RequestWrapper requestWrapper) throws InvalidProtocolBufferException {
        GetFileRequest request = GetFileRequest.parseFrom(requestWrapper.getRequest().getBody());
        try {
            String filename = request.getFilename();
            String path = filename;
            File file = new File(path);
         //   fileSendTask.execute(false);
        } catch (Exception e) {
        }
    }


    private void handleFileTransferRequest(RequestWrapper requestWrapper) {
        FilePacket filePacket = FilePacket.parseFrom(requestWrapper.getRequest().getBody());
        fileReceiveHandler.handleRequest(filePacket);
    }
}
